<template>
  <div>
    <workflow-create-dialog v-bind="$attrs" v-on="$listeners" ref="create-dialog"
                            class="document-dialog" @submit="createSubmit">
      <template slot-scope="{data}">
        <component :is="leftFormComponent" :row.sync="data" ref="leftForm"/>
      </template>
      <template slot-scope="{data}"  slot="center">
        <my-word :type="data.type_id" :row="data" :isEdit="true"></my-word>
      </template>
    </workflow-create-dialog>
    <approver-list :visible.sync="approverInfo.visible" v-if="approverInfo.visible" @callback="setApprover"
                   :data-list="approverInfo.dataList" :showCheckbox="approverInfo.showCheckbox"></approver-list>
  </div>
</template>
<script>
  import WorkflowCreateDialog from "@/components/WorkFlowsDialog/WorkflowCreateDialog";
  import ApproverList from "@/components/WorkFlowsDialog/Case/ApproverList";
  import eventBus from "@/utils/eventBus";
  import mixincommon from '@/mixins/common'
  import isSelectApprover from '@/mixins/isSelectApprover'
  import MyWord from "@/components/WorkFlowsDialog/DocumentCase/MyWord";
  export default {
    name: "CreateDocumentDialog",
    components: {MyWord, ApproverList, WorkflowCreateDialog},
    mixins: [mixincommon, isSelectApprover],
    data() {
      return {
        info: {},
        leftFormComponent: null,  // 定义组件
      }
    },
    computed: {},
    created() {
      this.getComponent()
    },
    methods: {
      getComponent() {
        let form = require('@/views/' + this.fullPath + '/LeftForm')
        this.leftFormComponent = form.default
      },
      submitForm() {
        if (this.info.id) {
          if (this.info.is_edit) {
            this.api.toEditAgain(this.info.id, this.info).then(res => {
              this.submitComplete(res)
            })
          } else {
            this.api.toEdit(this.info.id, this.info).then(res => {
              this.submitComplete(res)
            })
          }
        } else {
          this.api.toAdd(this.info).then(res => {
            this.submitComplete(res)
          })
        }
      },
      submitComplete(res) {
        this.msgSuccess(res.msg)
        if (this.info.is_draft) {
          eventBus.$emit('setActiveName', 'draft')
        } else if (this.info.is_edit) {
          eventBus.$emit('setActiveName', 'daiban')
        } else {
          eventBus.$emit('setActiveName', 'faqi')
        }
        eventBus.$emit('closeDetailDialog', true)
      },
      createSubmit(is_draft) {
        this.$refs['leftForm'].submitForm().then(data => {
          data.is_draft = is_draft
          this.info = data
          if (this.workFlowKey && is_draft == 0&&!this.info.is_edit) {
            this.getIsSelect(this.workFlowKey, () => {
              this.submitForm()
            })
          } else {
            this.submitForm()
          }
        })
      }
    }
  }
</script>
